// Copyright (c) Lawrence Livermore National Security, LLC and other VisIt
// Project developers.  See the top-level LICENSE file for dates and other
// details.  No copyright assignment is required to contribute to VisIt.

#include "QvisTessellateWindow.h"

#include <TessellateAttributes.h>

#include <QCheckBox>
#include <QLabel>
#include <QLayout>
#include <QLineEdit>


// ****************************************************************************
// Method: QvisTessellateWindow::QvisTessellateWindow
//
// Purpose: 
//   Constructor
//
// Note:       Autogenerated by xml2window.
//
// Programmer: xml2window
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

QvisTessellateWindow::QvisTessellateWindow(const int type,
                         TessellateAttributes *subj,
                         const QString &caption,
                         const QString &shortName,
                         QvisNotepadArea *notepad)
    : QvisOperatorWindow(type,subj, caption, shortName, notepad)
{
    atts = subj;
}


// ****************************************************************************
// Method: QvisTessellateWindow::~QvisTessellateWindow
//
// Purpose: 
//   Destructor
//
// Note:       Autogenerated by xml2window.
//
// Programmer: xml2window
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

QvisTessellateWindow::~QvisTessellateWindow()
{
}


// ****************************************************************************
// Method: QvisTessellateWindow::CreateWindowContents
//
// Purpose: 
//   Creates the widgets for the window.
//
// Note:       Autogenerated by xml2window.
//
// Programmer: xml2window
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

void
QvisTessellateWindow::CreateWindowContents()
{
    QGridLayout *mainLayout = new QGridLayout(0);
    topLayout->addLayout(mainLayout);

    chordErrorLabel = new QLabel(tr("Chord error"), central);
    mainLayout->addWidget(chordErrorLabel,0,0);
    chordError = new QLineEdit(central);
    connect(chordError, SIGNAL(editingFinished()),
            this, SLOT(chordErrorProcessText()));
    mainLayout->addWidget(chordError, 0,1);

    fieldCriterionLabel = new QLabel(tr("Field criterion"), central);
    mainLayout->addWidget(fieldCriterionLabel,1,0);
    fieldCriterion = new QLineEdit(central);
    connect(fieldCriterion, SIGNAL(editingFinished()),
            this, SLOT(fieldCriterionProcessText()));
    mainLayout->addWidget(fieldCriterion, 1,1);

    mergePoints = new QCheckBox(tr("Merge points"), central);
    connect(mergePoints, SIGNAL(toggled(bool)),
            this, SLOT(mergePointsChanged(bool)));
    mainLayout->addWidget(mergePoints, 2,0);

}


// ****************************************************************************
// Method: QvisTessellateWindow::UpdateWindow
//
// Purpose: 
//   Updates the widgets in the window when the subject changes.
//
// Note:       Autogenerated by xml2window.
//
// Programmer: xml2window
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

void
QvisTessellateWindow::UpdateWindow(bool doAll)
{

    for(int i = 0; i < atts->NumAttributes(); ++i)
    {
        if(!doAll)
        {
            if(!atts->IsSelected(i))
            {
                continue;
            }
        }

        switch(i)
        {
          case TessellateAttributes::ID_chordError:
            chordError->setText(DoubleToQString(atts->GetChordError()));
            break;
          case TessellateAttributes::ID_fieldCriterion:
            fieldCriterion->setText(DoubleToQString(atts->GetFieldCriterion()));
            break;
          case TessellateAttributes::ID_mergePoints:
            mergePoints->blockSignals(true);
            mergePoints->setChecked(atts->GetMergePoints());
            mergePoints->blockSignals(false);
            break;
        }
    }
}


// ****************************************************************************
// Method: QvisTessellateWindow::GetCurrentValues
//
// Purpose: 
//   Gets values from certain widgets and stores them in the subject.
//
// Note:       Autogenerated by xml2window.
//
// Programmer: xml2window
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

void
QvisTessellateWindow::GetCurrentValues(int which_widget)
{
    bool doAll = (which_widget == -1);

    // Do chordError
    if(which_widget == TessellateAttributes::ID_chordError || doAll)
    {
        double val;
        if(LineEditGetDouble(chordError, val))
            atts->SetChordError(val);
        else
        {
            ResettingError(tr("Chord error"),
                DoubleToQString(atts->GetChordError()));
            atts->SetChordError(atts->GetChordError());
        }
    }

    // Do fieldCriterion
    if(which_widget == TessellateAttributes::ID_fieldCriterion || doAll)
    {
        double val;
        if(LineEditGetDouble(fieldCriterion, val))
            atts->SetFieldCriterion(val);
        else
        {
            ResettingError(tr("Field criterion"),
                DoubleToQString(atts->GetFieldCriterion()));
            atts->SetFieldCriterion(atts->GetFieldCriterion());
        }
    }

}


//
// Qt Slot functions
//


void
QvisTessellateWindow::chordErrorProcessText()
{
    GetCurrentValues(TessellateAttributes::ID_chordError);
    Apply();
}


void
QvisTessellateWindow::fieldCriterionProcessText()
{
    GetCurrentValues(TessellateAttributes::ID_fieldCriterion);
    Apply();
}


void
QvisTessellateWindow::mergePointsChanged(bool val)
{
    atts->SetMergePoints(val);
    SetUpdate(false);
    Apply();
}


